<%= simple_form_for [:backend, @role], wrapper: :admin_theme, html: {'data-parsley-validate': true, class: 'uk-form uk-form-horizontal', autocomplete: 'off'} do |f| %>
  <%= f.input :name, label: '角色名', autofocus: true, required: true, placeholder: '' %>
  <hr>
  <h3>权限</h3>

  <%= render partial: "permissions", locals: {f: f, name: :permissions_attributes, permissions: @role.permissions} %>

  <hr>
  <button class="uk-button uk-button-success">提交</button>
<% end %>


<script>
    $(function () {
        $(".control").each(function () {
            let flag = true
            $(this).parent(".field").children(".control").children(".checkbox").each(function () {
                if (!$(this).is(':checked')) {
                    flag = false
                }
            })
            if (flag) {
                $(this).parent(".field").children("b").children(".select-all-btn").each(function () {
                    $(this).prop('checked', true)
                })
            }
        })
    })
    function selectAllPermission(which) {
        if ($(which).is(':checked')) {
            $(which).parent("b").parent(".field").children(".control").children(".checkbox").each(function () {
                $(this).prop("checked", true);
            })
        } else {
            $(which).parent("b").parent(".field").children(".control").children(".checkbox").each(function () {
                $(this).prop("checked", false);
            })
        }
    }
    function checkSelect(which) {
        // alert($(which).is(':checked'))
        if ($(which).is(':checked')) {
            let flag = true
            $(which).parent(".control").children(".checkbox").each(function(){
                if (!$(this).is(':checked')){
                    flag = false
                }
            })
            if (flag){
                $(which).parent(".control").parent(".field").children("b").children(".select-all-btn").each(function () {
                    $(this).prop('checked', true)
                })
            }
        } else {

            $(which).parent(".control").parent(".field").children("b").children(".select-all-btn").each(function () {
                $(this).prop('checked', false)
            })
        }
    }
</script>
